package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.UserRealname;
import com.heima.user.mapper.UseridentifyMapper;
import com.heima.user.service.UseridentifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class UseridentifyServiceImpl extends ServiceImpl<UseridentifyMapper, UserRealname> implements UseridentifyService {


    @Override
    public PageResponseResult pageList(AuthDto dto) {
        dto.checkParam();

        Page<UserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<UserRealname> queryWrapper = new LambdaQueryWrapper<>();
        //根据审核状态查询
        queryWrapper.eq(dto.getStatus() != null, UserRealname::getStatus, dto.getStatus());
        List<UserRealname> records = page(page, queryWrapper).getRecords();

        PageResponseResult result = new PageResponseResult();
        result.setData(records);
        result.setTotal(records.size());
        result.setCurrentPage(dto.getPage());
        result.setSize(dto.getSize());
        return  result;
    }

    @Override
    public ResponseResult authFail(AuthDto dto) {
        //根据id修改状态
        dto.checkParam();
        LambdaQueryWrapper<UserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRealname::getId, dto.getId());
        //根据Id修改状态,设置审核失败原因
        update(new UserRealname().setStatus(2).setReason(dto.getMsg()), queryWrapper);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult authPass(AuthDto dto) {
        dto.checkParam();
        //根据id修改状态为9，认证通过
        update(new UserRealname().setStatus(9), new LambdaQueryWrapper<UserRealname>().eq(UserRealname::getId, dto.getId()));
        return ResponseResult.okResult(null);
    }

}
